<?php 
if(KT!='KhiThe') exit;
class rates_controller extends rates_model{
		
	public function __construct(){
		global $lang;
		$lang['sms']='';
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
	}
	
	public function index_action()
	{
		$resultHtml = NULL;
		$strHtmlContent = $this->khithe_display('index');
		$arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultrate = parent::getrates();
		while( $row_rates = mysql_fetch_assoc($resultrate) )
		{
			$arrrate = array(
				"cell_ID"=>$row_rates['ID'],
				"cell_1"=>'<input '.($row_rates['Status']=="true"?"checked=\"checked\"":"").' name="delId" type="radio" value="'.$row_rates['ID'].'" />',
				"cell_2"=>$row_rates['Ratename'],
				"cell_3"=>$row_rates['Rate'],
				"cell_4"=>$row_rates['Status']=='true'?$this->lang['enabled']:$this->lang['disabled'],
				"cell_5"=>($row_rates['PostDate']!=''?date("d-m-Y", $row_rates['PostDate']):''),
				"class_cell"=>$row_rates['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
			);
			$resultHtml .= $this->khithe_assign($arrBlockHtml['rates'],$arrrate);
			
		} mysql_free_result($resultrate);
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		return $strHtmlContent;
	}
	
	public function add_action()
	{
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_Ratename'])){
				$ratename = parent::checkStrMySQL($_POST['txt_Ratename']);
				if($ratename==''){
					$this->lang['sms'] = $this->lang['Emptyrate'];
				
				}else{
					$done = parent::updateInsertrate($_POST);
					if($done) parent::gotowinparent(url_base.'admin/rates');
				}
			}
			
			$this->lang['txt_RateValue'] = isset($_POST['txt_Rate'])?$_POST['txt_Rate']:NULL;
			$this->lang['txt_RatenameValue'] =isset($_POST['txt_Ratename'])?$_POST['txt_Ratename']:NULL;
			$this->lang['active'] ='add';
			$this->lang['ratesID'] ='';
			$this->lang['titlePageEditor'] = $this->lang['addTitle'];
			$strHtmlContent = $this->khithe_display('editor');
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function edit_action()
	{
		if($_GET['iframe']=='true')
		{
			if(isset($_POST['txt_Ratename'])){
				$ratename = parent::checkStrMySQL($_POST['txt_Ratename']);
				if($ratename==''){
					$this->lang['sms'] = $this->lang['Emptyrate'];
				
				}else{
					$done = parent::updateInsertrate($_POST);
					if($done) parent::gotowinparent(url_base.'admin/rates');
				}
			}
			$resultrate = parent::getrateID($this->valUrl(1));
			$row_rates = mysql_fetch_assoc($resultrate);
			$strHtmlContent = $this->khithe_display('editor');
			$this->lang['titlePageEditor'] = $this->lang['editTitle'];
			
			$this->lang['txt_RateValue'] = isset($_POST['txt_Rate']) && $_POST['txt_Rate']!=''?$_POST['txt_Rate']:$row_rates['Rate'];
			$this->lang['txt_RatenameValue'] = isset($_POST['txt_Ratename']) && $_POST['txt_Ratename']!=''?$_POST['txt_Ratename']:$row_rates['Ratename'];
	
			$this->lang['active'] ='edit';
			$this->lang['ratesID'] =$row_rates['ID'];
			
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function index_ajax_action()
	{
		switch ($this->valUrl(1))
		{
			case 'open':
				$select = parent::getrates();
				while($row = mysql_fetch_assoc($select)){
					if($row['ID']==intval($this->valUrl(2))){
						$done = parent::setCheckedrate(intval($this->valUrl(2)),'true');
					}else{
						$done = parent::setCheckedrate($row['ID'],'false');
					}
				} mysql_free_result($select);
				
			break;
			case 'del':
				$done = parent::delCheckedrate(intval($this->valUrl(2)));	
			break;
		}
		if($this->valUrl(1)!=''&&isset($done)){
			return $this->index_action();
		}else{
			return 'notOK';
		}	
	}


}
?>